home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / SequenceInputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  6.2 KB  |  208 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)SequenceInputStream.java    1.20 98/06/29
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. import java.io.InputStream;
  18. import java.util.Enumeration;
  19. import java.util.Vector;
  20.  
  21. /**
  22.  * A <code>SequenceInputStream</code> represents
  23.  * the logical concatenation of other input
  24.  * streams. It starts out with an ordered
  25.  * collection of input streams and reads from
  26.  * the first one until end of file is reached,
  27.  * whereupon it reads from the second one,
  28.  * and so on, until end of file is reached
  29.  * on the last of the contained input streams.
  30.  *
  31.  * @author  Author van Hoff
  32.  * @version 1.20, 06/29/98
  33.  * @since   JDK1.0
  34.  */
  35. public
  36. class SequenceInputStream extends InputStream {
  37.     Enumeration e;
  38.     InputStream in;
  39.  
  40.     /**
  41.      * Initializes a newly created <code>SequenceInputStream</code>
  42.      * by remembering the argument, which must
  43.      * be an <code>Enumeration</code>  that produces
  44.      * objects whose run-time type is <code>InputStream</code>.
  45.      * The input streams that are  produced by
  46.      * the enumeration will be read, in order,
  47.      * to provide the bytes to be read  from this
  48.      * <code>SequenceInputStream</code>. After
  49.      * each input stream from the enumeration
  50.      * is exhausted, it is closed by calling its
  51.      * <code>close</code> method.
  52.      *
  53.      * @param   e   an enumeration of input streams.
  54.      * @see     java.util.Enumeration
  55.      */
  56.     public SequenceInputStream(Enumeration e) {
  57.     this.e = e;
  58.     try {
  59.         nextStream();
  60.     } catch (IOException ex) {
  61.         // This should never happen
  62.         throw new Error("panic");
  63.     }
  64.     }
  65.  
  66.     /**
  67.      * Initializes a newly
  68.      * created <code>SequenceInputStream</code>
  69.      * by remembering the two arguments, which
  70.      * will be read in order, first <code>s1</code>
  71.      * and then <code>s2</code>, to provide the
  72.      * bytes to be read from this <code>SequenceInputStream</code>.
  73.      *
  74.      * @param   s1   the first input stream to read.
  75.      * @param   s2   the second input stream to read.
  76.      */
  77.     public SequenceInputStream(InputStream s1, InputStream s2) {
  78.     Vector    v = new Vector(2);
  79.  
  80.     v.addElement(s1);
  81.     v.addElement(s2);
  82.     e = v.elements();
  83.     try {
  84.         nextStream();
  85.     } catch (IOException ex) {
  86.         // This should never happen
  87.         throw new Error("panic");
  88.     }
  89.     }
  90.  
  91.     /**
  92.      *  Continues reading in the next stream if an EOF is reached.
  93.      */
  94.     final void nextStream() throws IOException {
  95.     if (in != null) {
  96.         in.close();
  97.     }
  98.  
  99.         if (e.hasMoreElements()) {
  100.             in = (InputStream) e.nextElement();
  101.             if (in == null)
  102.                 throw new NullPointerException();
  103.         }
  104.         else in = null;
  105.  
  106.     }
  107.  
  108.     /**
  109.      * Returns the number of bytes available on the current stream.
  110.      *
  111.      * @since   JDK1.1
  112.      */
  113.     public int available() throws IOException {
  114.     if(in == null) {
  115.         return 0; // no way to signal EOF from available()
  116.     }
  117.     return in.available();
  118.     }
  119.  
  120.     /**
  121.      * Reads the next byte of data from this input stream. The byte is
  122.      * returned as an <code>int</code> in the range <code>0</code> to
  123.      * <code>255</code>. If no byte is available because the end of the
  124.      * stream has been reached, the value <code>-1</code> is returned.
  125.      * This method blocks until input data is available, the end of the
  126.      * stream is detected, or an exception is thrown.
  127.      * <p>
  128.      * This method
  129.      * tries to read one character from the current substream. If it
  130.      * reaches the end of the stream, it calls the <code>close</code>
  131.      * method of the current substream and begins reading from the next
  132.      * substream.
  133.      *
  134.      * @return     the next byte of data, or <code>-1</code> if the end of the
  135.      *             stream is reached.
  136.      * @exception  IOException  if an I/O error occurs.
  137.      */
  138.     public int read() throws IOException {
  139.     if (in == null) {
  140.         return -1;
  141.     }
  142.     int c = in.read();
  143.     if (c == -1) {
  144.         nextStream();
  145.         return read();
  146.     }
  147.     return c;
  148.     }
  149.  
  150.     /**
  151.      * Reads up to <code>len</code> bytes of data from this input stream
  152.      * into an array of bytes. This method blocks until at least 1 byte
  153.      * of input is available. If the first argument is <code>null</code>,
  154.      * up to <code>len</code> bytes are read and discarded.
  155.      * <p>
  156.      * The <code>read</code> method of <code>SequenceInputStream</code>
  157.      * tries to read the data from the current substream. If it fails to
  158.      * read any characters because the substream has reached the end of
  159.      * the stream, it calls the <code>close</code> method of the current
  160.      * substream and begins reading from the next substream.
  161.      *
  162.      * @param      b     the buffer into which the data is read.
  163.      * @param      off   the start offset of the data.
  164.      * @param      len   the maximum number of bytes read.
  165.      * @exception  IOException  if an I/O error occurs.
  166.      */
  167.     public int read(byte b[], int off, int len) throws IOException {
  168.     if (in == null) {
  169.         return -1;
  170.     } else if (b == null) {
  171.         throw new NullPointerException();
  172.     } else if ((off < 0) || (off > b.length) || (len < 0) ||
  173.            ((off + len) > b.length) || ((off + len) < 0)) {
  174.         throw new IndexOutOfBoundsException();
  175.     } else if (len == 0) {
  176.         return 0;
  177.     }
  178.  
  179.     int n = in.read(b, off, len);
  180.     if (n <= 0) {
  181.         nextStream();
  182.         return read(b, off, len);
  183.     }
  184.     return n;
  185.     }
  186.  
  187.     /**
  188.      * Closes this input stream and releases any system resources
  189.      * associated with the stream.
  190.      * A closed <code>SequenceInputStream</code>
  191.      * cannot  perform input operations and cannot
  192.      * be reopened.
  193.      * <p>
  194.      * If this stream was created
  195.      * from an enumeration, all remaining elements
  196.      * are requested from the enumeration and closed
  197.      * before the <code>close</code> method returns.
  198.      * of <code>InputStream</code> .
  199.      *
  200.      * @exception  IOException  if an I/O error occurs.
  201.      */
  202.     public void close() throws IOException {
  203.     do {
  204.         nextStream();
  205.     } while (in != null);
  206.     }
  207. }
  208.